package com.alexvas.rtsp.codec

import android.content.Context
import android.graphics.SurfaceTexture
import android.util.AttributeSet
import android.view.Surface
import android.view.TextureView
import com.alexvas.rtsp.codec.renderer.VideoRenderThread

class VideoTextureView(context: Context, attrs: AttributeSet? = null)
    : TextureView(context, attrs), TextureView.SurfaceTextureListener {

    private var renderThread: VideoRenderThread? = null

    init {
        surfaceTextureListener = this
    }

    override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
        renderThread = VideoRenderThread(surface, width, height){

        }.apply { start() }
    }

    override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {}
    override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
        renderThread?.releaseThread()
        renderThread = null
        return true
    }

    override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {}

    /** 提供给外部使用，返回 MediaCodec 可用的 Surface */
    fun getCodecSurface(): Surface? = renderThread?.codecSurface
}
